package com.qzdsoft.erpcloud.controller.stock;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.stock.StockInbound;
import com.qzdsoft.erpcloud.domain.stock.StockOutbound;
import com.qzdsoft.erpcloud.domain.stock.StockStore;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.proj.MaterialService;
import com.qzdsoft.erpcloud.service.stock.StockInboundDetailService;
import com.qzdsoft.erpcloud.service.stock.StockInboundService;
import com.qzdsoft.erpcloud.service.stock.StockOutBoundService;
import com.qzdsoft.erpcloud.service.stock.StockStoreService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.proj.MaterialReviewStatus;
import com.qzdsoft.vo.pu.InboundType;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;
/**
 * 销售退货入库单
 * @author pc-20170422
 *
 */
@Controller
@RequestMapping("/stock/saleinbound")
public class StockSaleInboundController {
    
    private static final String BASEPATH = "stock/saleinbound";
    private static final Logger logger = LoggerFactory.getLogger(StockSaleInboundController.class);
    
    @Autowired
    private StockInboundService stockInboundService;
    @Autowired
    private CustomerSupplierService customerSupplierService;
    @Autowired
    private StockOutBoundService stockOutBoundService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private StockInboundDetailService stockInboundDetailService;
    @Autowired
    StockStoreService stockStoreService;
    /**
     * 销售退货单首页
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/index");
        return mv;
    }
    /**
     * 销售退货单条件查询
     * @param info
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<StockInboundInfo> list(StockInboundQueryInfo info){
    	info.setInboundType(Integer.parseInt(InboundType.SALE_CODE));
    	logger.debug("入库类型:type={}",info.getInboundType());
    	//如果初始化查询返回无数据空页
        if(info.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
        	return stockInboundService.findStockInboundOrdersBy(info);
        }
    }
    /**
     * 打开编辑页面
     * @param companyid
     * @param id
     * @return
     */
    /*@RequestMapping("/{companyid}/{id}")
    public ModelAndView toedit(@PathVariable("companyid")String companyid,@PathVariable("id")Integer id) {
        logger.debug("查询采购入库单详情：companyid={},id={}",companyid,id);
        ModelAndView mv = new ModelAndView();
        StockInboundInfo order = stockInboundService.findStockInboundOrderBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/edit");
        return mv;
    }*/
    /**
     * 采购入库单修改保存
     * @param info
     * @return
     */
   /* @ResponseBody
    @RequestMapping("/edit")
    public Response<StockInboundInfo> edit(StockInbound info) {
        logger.info(info.getId()+","+info.getRemark());
        logger.debug("采购入库单编辑:id={}",info.getId());
        return stockInboundService.edit(info);
    }*/
    /**
     * 打开销售退货新增页面
     * @param companyid
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView toadd(Integer companyid) {
    	logger.debug("销售退货入库单新增：companyid={}",companyid);
    	logger.debug(companyid.toString());
    	//查询销售出库单
    	StockOutbound out = new StockOutbound();
    	out.setCompanyId(companyid);
    	out.setReviewStatus(1);
    	List<StockOutbound> orders = stockOutBoundService.selectReviewOrderByCompanyId(out);
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("orders", orders);
    	mv.setViewName(BASEPATH+"/add");
    	return mv;
    }
    
    /**
     * 根据来源单号查询详情
     * @param id
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/purchaseOrder/{id}")
    public Response getpurchaseOrderById(@PathVariable("id")Integer id) {
    	//查询来源单据号
    	logger.debug("查询来源单据号:sourceid={}",id);
//    	return stockInboundService.selectBySourceId(id);
    	return stockInboundService.selectByOutOrderId(id);
    }
    /**
     * 新增表头保存
     * @param info
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/add")
    public Response<String> addSave(StockInbound info) {
    	logger.info(info.getBillNo()+":"+info.getRemark());
    	logger.debug("销售退货入库单新增保存:");
    	//销售退货入库类型
    	info.setInboundType(Integer.parseInt(InboundType.SALE_CODE));
    	return stockInboundService.save(info);
    }
    
    /**
     * 销售退货明细查看
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/detail/index")
    public ModelAndView index(@RequestParam(value="id") Integer id) {
    	//查询采购入库单
    	StockInboundInfo  info= stockInboundService.findStockInboundOrderBy(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("StockInboundOrder", info);
      /*  List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);*/
        mv.setViewName(BASEPATH+"/detail");
        return mv;
    }
    
    /**
     * 明细列表数据
     * @param id
     * @param queryInfo
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/order-details")
    public ModelAndView item(StockInboundDetailQueryInfo queryInfo) {
    	logger.debug("采购订单明细:id={},companyId={}",queryInfo.getId(),queryInfo.getCompanyid());
    	ModelAndView mv = new ModelAndView();
    	 //查询物料信息
        MaterialQueryInfo info = new MaterialQueryInfo();
        info.setIsBuy(true);//采购的物料
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        List<MaterialInfo> materialInfos = materialService.selectBy(info);
    	mv.addObject("materialInfos", materialInfos);
    	//查询采购入库单
    	StockInboundInfo  InboundOrder= stockInboundService.findStockInboundOrderBy(queryInfo.getId());
    	logger.debug("InboundOrder:id={}",InboundOrder.getId());
    	mv.addObject("InboundOrder", InboundOrder);
    	//公司仓库信息
    	List<StockStore> stockStore = stockStoreService.selectByComId(queryInfo.getCompanyid());
        mv.addObject("stockStore",stockStore);
    	//采购入库单明细
    	List<StockInboundDetailInfo> details = stockInboundDetailService.selectDetailBy(queryInfo);
    	mv.addObject("details", details);
    	mv.setViewName(BASEPATH + "/detailItem");
    	return mv;
    }
}
